package com.xxcxcxcx.jobsystem.mixin.job.Cook;

import com.xxcxcxcx.jobsystem.Main;
import com.xxcxcxcx.jobsystem.common.Job.Blacksmith.Blacksmith;
import com.xxcxcxcx.jobsystem.common.Job.Cook.Cook;
import com.xxcxcxcx.jobsystem.common.Job.Cook.InventoryIn;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static com.xxcxcxcx.jobsystem.common.Job.Blacksmith.Blacksmith.*;
import static com.xxcxcxcx.jobsystem.common.Job.Cook.Cook.*;

@Mixin(Inventory.class)
public abstract class InventoryMixin implements InventoryIn , Container {
    @Shadow @Final public Player player;

    @Shadow public abstract ItemStack getItem(int p_35991_);

    @Shadow public abstract void setItem(int p_35999_, ItemStack p_36000_);

    @Inject(method = "setItem", at = @At(value = "HEAD"),cancellable = true)
    private void CookSet(int p_35999_, ItemStack p_36000_, CallbackInfo ci) {
        Player player = this.player;
        CompoundTag nbt= p_36000_.getOrCreateTagElement(Main.MOD_ID);
        if(nbt.isEmpty()){
            p_36000_.removeTagKey(Main.MOD_ID);
        }
        else{
            if(nbt.getString(MakeFood+FoodLevel).equals(UnIdentification)){
                if(nbt.getString(MakeFood+Cooker).equals(player.getName().getContents())){
                    if(player instanceof ServerPlayer){
                        while(!p_36000_.isEmpty()){
                            Cook.IdentificationFood(player, p_36000_);
                        }
                    }
                    ci.cancel();
                }
            }
            if(nbt.getString(MakeTool+ToolLevel).equals(ToolUnIdentification)){
                if(nbt.getString(MakeTool+Creater).equals(player.getName().getContents())){
                    if(player instanceof ServerPlayer){
                        while(!p_36000_.isEmpty()){
                            Blacksmith.IdentificationTool(player, p_36000_);
                        }
                    }
                    ci.cancel();
                }
            }
        }
    }
    @Override
    public int addResource(int p_36048_, ItemStack p_36049_) {
        Player player = this.player;
        CompoundTag nbt= p_36049_.getOrCreateTagElement(Main.MOD_ID);
        if(nbt.isEmpty()){
            p_36049_.removeTagKey(Main.MOD_ID);
        }else{
            if(nbt.getString(MakeFood+FoodLevel).equals(UnIdentification)){
                if(nbt.getString(MakeFood+Cooker).equals(player.getName().getContents())){
                    if(player instanceof ServerPlayer){
                        while(!p_36049_.isEmpty()){
                            Cook.IdentificationFood(player, p_36049_);
                        }
                    }
                    return 0;
                }else{
                    return p_36049_.getCount();
                }
            }
            if(nbt.getString(MakeTool+ToolLevel).equals(ToolUnIdentification)){
                if(nbt.getString(MakeTool+Creater).equals(player.getName().getContents())){
                    if(player instanceof ServerPlayer){
                        while(!p_36049_.isEmpty()){
                            Blacksmith.IdentificationTool(player, p_36049_);
                        }
                    }
                    return 0;
                }else{
                    return p_36049_.getCount();
                }
            }
        }
        Item item = p_36049_.getItem();
        int i = p_36049_.getCount();
        ItemStack itemstack = this.getItem(p_36048_);
        if (itemstack.isEmpty()) {
            itemstack = p_36049_.copy(); // Forge: Replace Item clone above to preserve item capabilities when picking the item up.
            itemstack.setCount(0);
            if (p_36049_.hasTag()) {
                itemstack.setTag(p_36049_.getTag().copy());
            }

            this.setItem(p_36048_, itemstack);
        }

        int j = Math.min(i, itemstack.getMaxStackSize() - itemstack.getCount());

        if (j > this.getMaxStackSize() - itemstack.getCount()) {
            j = this.getMaxStackSize() - itemstack.getCount();
        }

        if (j != 0) {
            i -= j;
            itemstack.grow(j);
            itemstack.setPopTime(5);
        }
        return i;
    }
}
